package com.shop.zzh.dto;

import com.shop.zzh.entity.User;
import com.shop.zzh.mbg.model.UserPermission;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: zzh
 * @description: SpringSecurity需要的用户详情
 * @author: zhuzh
 * @create: 2019-12-16 22:21
 **/
public class AdminUserDetails implements UserDetails {
	private User user;
	private List<UserPermission> permissionList;
	public AdminUserDetails(User user, List<UserPermission> permissionList) {
		this.user = user;
		this.permissionList = permissionList;
	}
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		//返回当前用户的权限
		return permissionList.stream()
				.filter(userPermission -> userPermission.getPermissionName()!=null)
				.map(userPermission ->new SimpleGrantedAuthority(userPermission.getPermissionName()))
				.collect(Collectors.toList());
	}

	@Override
	public String getPassword() {
		return user.getPassword();
	}

	@Override
	public String getUsername() {
		return user.getUserName();
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return user.getStatus().equals(1);
	}
}
